package com.gitee.wsl.compose.ui.section.lazylist

import androidx.compose.animation.EnterTransition
import androidx.compose.animation.ExitTransition
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.lazy.LazyItemScope
import androidx.compose.foundation.lazy.LazyListScope
import androidx.compose.foundation.shape.CornerBasedShape
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.takeOrElse
import com.gitee.wsl.compose.ui.section.CupertinoSectionDefaults
import com.gitee.wsl.compose.ui.section.CupertinoSectionTokens
import com.gitee.wsl.compose.ui.section.LocalSectionStyle
import com.gitee.wsl.compose.ui.section.SectionState
import com.gitee.wsl.compose.ui.section.SectionStyle
import com.gitee.wsl.compose.ui.section.SectionTitle

/**
 * Section extension for lazy lists.
 *
 * Section [title], [caption] and each row will be rendered as a separate lazy list item.
 * There is also a [CupertinoSection] variant for usage outside of lazy lists.
 *
 * @param style style of the section. If no value provided [LocalSectionStyle] will be used.
 * @param title section top label. To match the section style, title string should be transformed with [sectionTitle]
 * @param caption section bottom text.
 * @param content section builder.
 * @param state section state. Used for section collapsing
 * when [style] is [SectionStyle.Sidebar] and [title] is provided.
 * @param enterTransition section collapse animation
 * @param exitTransition section expand animation
 * @see CupertinoSection
 * @see sectionTitle
 * */

fun LazyListScope.Section(
    style: SectionStyle? = null,
    state: SectionState? = null,
    enterTransition: EnterTransition = CupertinoSectionDefaults.EnterTransition,
    exitTransition: ExitTransition = CupertinoSectionDefaults.ExitTransition,
    shape : CornerBasedShape?= null,
    color : Color = Color.Unspecified,
    title: @Composable (LazyItemScope.() -> Unit)? = null,
    caption: @Composable (LazyItemScope.() -> Unit)? = null,
    content: LazySectionScope.() -> Unit
) {

    val itemsPadding = CupertinoSectionDefaults.PaddingValues

    @Composable
    fun resolvedStyle(): SectionStyle = style ?: LocalSectionStyle.current

    @Composable
    fun resolvedColor(): Color = color.takeOrElse {
        CupertinoSectionDefaults.Color
    }


    item(contentType = SplitPaddingContentType) {
        Spacer(
            Modifier
                .height(CupertinoSectionTokens.SplitPadding)
                .fillMaxWidth()
        )
    }

    if (title != null) {
        item(contentType = SectionTitleContentType) {
            SectionTitle(
                style = resolvedStyle(),
                lazy = true,
                state = state,
            ) {
                title()
            }
        }
    }

    itemsAndCaption(
        resolvedStyle = { resolvedStyle() },
        resolvedColor = { resolvedColor() },
        enterTransition = enterTransition,
        exitTransition = exitTransition,
        state = state,
        shape = shape,
        itemsPadding = itemsPadding,
        caption = caption,
        content = content
    )
}